//
//  CommonClassReportViewController.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2018/1/29.
//  Copyright © 2018年 zongquankeji. All rights reserved.
//

#import "CommonClassReportViewController.h"
#import "CommonClassReportTableViewCell.h"
#import "CommonClassReportHeadView.h"
#import "CommonClassReportSectionHeadView.h"
#import "CommonClassReportSectionFootView.h"
#import "VipCardReportFilterView.h"


@interface CommonClassReportViewController ()<UITableViewDataSource,UITableViewDelegate,CommonClassReporHeadViewDelegate>

@property(nonatomic,weak)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,weak)CommonClassReportHeadView *headView;

@property(nonatomic,strong)CommonClassReportSectionFootView *footView;
@property(nonatomic,strong)CommonClassReportSectionHeadView *sectionHead;
@property(nonatomic,strong)NoDataView *noDataView;

@property(nonatomic,strong)NSMutableDictionary *filtDict;

@end

@implementation CommonClassReportViewController
-(NSMutableDictionary *)filtDict
{
    
    if (!_filtDict) {
        _filtDict = [NSMutableDictionary dictionary];
    }
    return _filtDict;
    
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title = @"课程报表";
    
    [self createSubView];
    
    self.view.backgroundColor = BackGroundColor;
    
    [self loadData];
}

-(void)createSubView{
    
    UIView *btnView = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth/4, CZQ_StatusBarHeight, kScreenWidth/2, 44)];
    
    self.navigationItem.titleView = btnView;
    
    //nav的两个按钮
    NSArray *btnArr = @[@"团课",@"私教"];
    
    for (int i = 0; i < btnArr.count; i ++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [btn setTitle:btnArr[i] forState:UIControlStateNormal];
        
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        
        [btn setTitleColor:CSBlackColor forState:UIControlStateSelected];
        
        [btn addTarget:self action:@selector(btnClick:)];
        
        btn.tag = 10 + i;
        
        
        btn.frame = CGRectMake(i * kScreenWidth/4, 0, kScreenWidth/4, 44);
        
        [btnView addSubview:btn];
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(kScreenWidth/16, 40, kScreenWidth/8, 2)];
        
        
        view.backgroundColor = ButtonBackGroundColor;
        
        view.tag = 20 + i;
        
        view.hidden = YES;
        
        [btn addSubview:view];
        
    
        
        if (i == 0) {

            btn.selected = YES;
            
            view.hidden = NO;


        }
        
        
    }
    
    
    //头部
    
    CommonClassReportHeadView *headView = [[CommonClassReportHeadView alloc] init];
    headView.commonClassReporHeadViewDelegate = self;
    [self.view addSubview:headView];
    self.headView = headView;
    [headView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.left.width.mas_equalTo(self.view);
    }];
    [headView.fitBtn addTarget:self action:@selector(filterBtnClick)];
    
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.estimatedRowHeight = 200;
    [tableView registerClass:[CommonClassReportTableViewCell class] forCellReuseIdentifier:@"CommonClassReportTableViewCell"];
    tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    tableView.backgroundColor = CSClearColor;
    [self.view addSubview:tableView];
    self.tableView = tableView;
    [tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(headView.mas_bottom).offset(10);
        
        make.left.width.bottom.mas_equalTo(self.view);
    }];
    
}

-(void)loadData{
    
    //GET_SalerReport
    
    [MBProgressHUD showMessage:@"加载中..." toView:self.view];
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    
    [params setObject:[[NSUserDefaults standardUserDefaults]objectForKey:@"UserID"] forKey:@"teacherid"];
    
    //判断团课还是私教
    UIView *view1 = [self.navigationItem.titleView viewWithTag:20];
    
    if (view1.hidden == YES) {
        [params setObject:self.headView.timeLabel1.text forKey:@"私教"];
    }else
    {
        [params setObject:self.headView.timeLabel1.text forKey:@"团课"];
        
    }
    
    [params setObject:self.headView.timeLabel1.text forKey:@"coursetype"];
    
    [params setObject:self.headView.timeLabel1.text forKey:@"beginDate"];
    
    [params setObject:self.headView.timeLabel2.text forKey:@"endDate"];
    
//    [params setDictionary:self.filtDict];
    
    [HTTPTool GET:GET_SalerReport_ByTearch parameters:params
     
          success:^(id responseObject){
              
              NSLog(@"%@",responseObject);
              
              [MBProgressHUD hideHUDForView:self.view];
              
              if (responseObject && [[responseObject objectForKey:@"Code"] intValue] == 0) {
                  
                  NSMutableArray *resultArray = [responseObject objectForKey:@"rows"];
                  
                  if (resultArray.count == 0) {
                      
                      self.noDataView = [NoDataView shareInstance];
                      
                      [self.noDataView hideInView:self.view];
                  }
                  
                  self.dataSource = [NSMutableArray arrayWithArray:resultArray];
                  
                  //判断团课还是私教
                  
                  self.sectionHead.classLabel.text = view1.hidden?@"私教(节)":@"团课(节)";
                  
                  self.sectionHead.classLabel1.text = [NSString stringWithFormat:@"%ld",self.dataSource.count];
                  
                  //未完成
                  int noCount = 0;
                  
                  for (NSDictionary *dict in self.dataSource) {
                      
                      if ([[dict objectForKey:@"IsNew"] intValue]) {
                          noCount = noCount + 1;
                      }
                      
                  }
                  
                  self.sectionHead.noClassLabel1.text = [NSString stringWithFormat:@"%ld",noCount];
                  
                  [self.tableView reloadData];
                  
              }
              
          }
     
          failure:^(NSError *error){
              
              [MBProgressHUD hideHUDForView:self.view];
          }];
    
}
#pragma mark 过滤
-(void)filterBtnClick{
    
    __weak typeof(self)weakSelf = self;
    
    VipCardReportFilterView *vipFilterView = [[VipCardReportFilterView alloc] init];
    vipFilterView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    vipFilterView.filterBlock = ^(NSMutableDictionary *filterDict) {
        
        weakSelf.filtDict = filterDict;
        
        [weakSelf loadData];
    };
    
    [[UIApplication sharedApplication].keyWindow addSubview:vipFilterView];
}

#pragma mark UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    //    return 10;
    return self.dataSource.count;
}

#pragma mark UITableViewDelegate
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    CommonClassReportSectionHeadView *headView = [[CommonClassReportSectionHeadView alloc] init];
    
    //读取保存的数据
    UserAccount *acount = [UserAccount account];
    if (acount) {
        [headView.iconImage sd_setImageWithURL:[Helper imageUrlStringToChinese:acount.Pic imageType:nil] placeholderImage:PlaceHold];
        headView.nameLabel.text = acount.Name;
        headView.sexImage.image = [acount.Sex integerValue] == 0?[UIImage imageNamed:@"icon_man"]:[UIImage imageNamed:@"icon_women"];
        headView.telLabel.text = acount.CellPhone;
    }
    
    self.sectionHead = headView;
    

    
    return headView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    
//    self.footView = [[CommonClassReportSectionFootView alloc] init];
//    
//    int totalCount = 0;
//    int totalFee = 0;
//    int totalBonus = 0;
//    
//    for (NSDictionary *dict in self.dataSource) {
//        
//        totalCount += [[dict objectForKey:@"GroupClass"] intValue];
//        
//        totalFee += [[dict objectForKey:@"PersonalTraining"] intValue];
//        
//        totalBonus += [[dict objectForKey:@"Total"] intValue];
//    }
//    
//    self.footView.countLabel.text = [NSString stringWithFormat:@"%d节",totalCount];
//    
//    self.footView.netReceiptLabel.text = [NSString stringWithFormat:@"%d节",totalFee];
//    
//    self.footView.receiveLabel.text = [NSString stringWithFormat:@"%d节",totalBonus];
    
    return [[UIView alloc]initWithFrame:CGRectZero];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 70;
}
-(CGFloat)tableView:(UITableView *)tableView estimatedHeightForHeaderInSection:(NSInteger)section
{
    
    return 200;
}

//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    
//    return 70;
//}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 70;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    CommonClassReportTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CommonClassReportTableViewCell"];
    
    
    NSDictionary *dict = [self.dataSource objectAtIndex:indexPath.row];
    
    cell.typeLabel.text = [dict objectForKey:@"JoinTime"];
    
    cell.countLabel.text= [NSString stringWithFormat:@"%@",[dict objectForKey:@"CourseName"]];
    
    cell.netReceiptLabel.text= [NSString stringWithFormat:@"%@",[dict objectForKey:@"TypeID"]];
    
    cell.receiveLabel.text= [NSString stringWithFormat:@"%@",[[dict objectForKey:@"IsNew"] integerValue] == 1?@"未完成":@"完成" ];;
    
    if (indexPath.row % 2 == 0) {
        
        cell.backgroundColor = CSColorFromRGB(0xf9f9f9);
    }
    else{
        
        cell.backgroundColor = CSWhiteColor;
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    
}


#pragma mark VipCardReportHeadViewDelegate
-(void)operateClick{
    
    [self loadData];
}

#pragma mark Get
-(NSMutableArray *)dataSource{
    
    if (!_dataSource) {
        
        _dataSource = [[NSMutableArray alloc] init];
    }
    
    return _dataSource;
}
#pragma mark 团课私教选择
-(void)btnClick:(UIButton *)sender
{
    
    sender.selected = YES;
    
    UIButton * btn1 = (UIButton *)[self.navigationItem.titleView viewWithTag:10];
    
    UIButton * btn2 = (UIButton *)[self.navigationItem.titleView viewWithTag:11];
    
    UIView *view1 = [self.navigationItem.titleView viewWithTag:20];
    
    UIView *view2 = [self.navigationItem.titleView viewWithTag:21];
    
    btn1.selected = NO;
    
    btn2.selected = NO;
    
    view1.hidden = YES;
    
    view2.hidden = YES;
    
    sender.selected = YES;
    
    if (sender.tag == 10) {
        
        view1.hidden = NO;
        
    }else{
        
        view2.hidden = NO;
        
    }
    
    [self loadData];
    
    
}

@end
